package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyHouseManagerUrl)
public class MyHouseManagerActivity extends BaseMvpActivity<MyHousePresenter> implements MineContract.HouseView {

    private static final int REQ_ADD = 101;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.container_right)
    ViewGroup containerRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;

    private MyHouseManagerAdapter mManagerAdapter;

    @Autowired
    String address;
    @Autowired
    int roomId;

    private int delPos = -1;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_list_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        containerRight.setVisibility(View.VISIBLE);
        tvTitle.setText("成员管理");
        tvRight.setText("添加");
        tvRight.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        containerRight.setOnClickListener(v ->
                ARouter.getInstance().build(ARouterUrlKt.MyHouseMemberAdd)
                        .withInt("roomId", roomId)
                        .navigation(this, REQ_ADD));

        mRefreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.fetchHouseMember(roomId));

        mRefreshLayout.setEnableLoadMore(false);
        mManagerAdapter = new MyHouseManagerAdapter();
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRvList.addItemDecoration(new SpaceItemDecoration(15));
        mRvList.setAdapter(mManagerAdapter);
        mManagerAdapter.setAddress(address);

        View emptyView = View.inflate(this, R.layout.empty_layout_house_member_list, null);
        View btnAdd = emptyView.findViewById(R.id.qbt_house_add_member);
        btnAdd.setOnClickListener(v -> ARouter.getInstance().build(ARouterUrlKt.MyHouseMemberAdd)
                .withInt("roomId", roomId)
                .navigation(this, REQ_ADD));
        mManagerAdapter.setEmptyView(emptyView);

        mManagerAdapter.setOnItemChildClickListener((adapter, view, position) ->
        {
            delPos = position;
            mPresenter.delMember(mManagerAdapter.getItem(position).getOwnerRoomId());
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        showLoading();
        mPresenter.fetchHouseMember(roomId);
    }

    @Override
    public void onFetchHouse(List<MyHouseResult> results) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == MyHousePresenter.DEL) {
            if (delPos >= 0) {
                mManagerAdapter.removeAt(delPos);
                delPos = -1;
            }
        }
    }

    @Override
    public void onFetchHouseMember(List<MyHouseMemberResult> results) {
        hideLoading();
        mRefreshLayout.finishRefresh(true);
        mManagerAdapter.setList(results);
    }

    @Override
    public void onFetchDoorAccess(DoorAccessDeviceResult result) {

    }

    @Override
    public void onFetchDoorRecord(List<DoorAccessRecordResult> results) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQ_ADD == requestCode) {
            if (resultCode == RESULT_OK) {
                mPresenter.fetchHouseMember(roomId);
            }
        }
    }
}
